<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:marc="http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="marc">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="record">
    <doc>
      <field name="medium">Electronic</field>
      <xsl:apply-templates select="controlfield"></xsl:apply-templates>
      <xsl:apply-templates select="datafield"></xsl:apply-templates>
    </doc>
  </xsl:template>

  <!-- Suppress all unmatched text/attributes -->
  <xsl:template match="text()|@*"/>

  <xsl:template match="controlfield[@tag='001']">
    <field name="identifier">
      URN:MetalibIRD:<xsl:value-of select="."/>
    </field>
  </xsl:template>
  <xsl:template match="datafield[@tag='245']">
    <field name="title">
      <xsl:call-template name="subfieldSelect">
        <xsl:with-param name="codes">abfghk</xsl:with-param>
      </xsl:call-template>
    </field>
  </xsl:template>
  <xsl:template match="datafield[@tag='110']">
    <field name="publisher"><xsl:call-template name="subfieldSelect"></xsl:call-template></field>
  </xsl:template>
  <xsl:template match="datafield[@tag='655']">
    <field name="type"><xsl:call-template name="subfieldSelect"></xsl:call-template></field>
    <field name="format"><xsl:call-template name="subfieldSelect"></xsl:call-template></field>
  </xsl:template>
  <xsl:template match="datafield[@tag='520']">
    <field name="summary">
      <xsl:value-of select="subfield[@code='a']"/>
    </field>
  </xsl:template>
  <xsl:template match="datafield[@tag='500' or @tag='505']">
    <field name="description">
      <xsl:value-of select="subfield[@code='a']"/>
    </field>
  </xsl:template>
  <xsl:template match="datafield[@tag='531']">
    <field name="description">
      <xsl:value-of select="subfield[@code='a']"/>
    </field>
  </xsl:template>
  <xsl:template match="datafield[@tag='540']">
    <field name="rights">
      <xsl:value-of select="subfield[@code='a']"/>
    </field>
  </xsl:template>
  <xsl:template match="datafield[@tag='546']">
    <field name="language">
      <xsl:value-of select="subfield[@code='a']"/>
    </field>
  </xsl:template>
  <xsl:template match="datafield[@tag='594']">
    <field name="status">
      <xsl:value-of select="subfield[@code='a']"/>
    </field>
  </xsl:template>
  <xsl:template match="datafield[@tag='595']">
    <field name="description">
      <xsl:value-of select="subfield[@code='a']"/>
    </field>
  </xsl:template>
  <xsl:template match="datafield[@tag='653' or @tag='710']">
    <field name="description">
      <xsl:value-of select="subfield[@code='a']"/>
    </field>
  </xsl:template>

  <xsl:template match="datafield[@tag='856' and @ind1='4']">
    <xsl:choose>
      <xsl:when test="@ind2='1'">
        <field name="url"><xsl:value-of select="subfield[@code='u']"/></field>
      </xsl:when>
      <xsl:when test="@ind2='2'">
        <field name="url"><xsl:value-of select="subfield[@code='u']"/></field>
      </xsl:when>
      <xsl:when test="@ind2='3'">
        <field name="url"><xsl:value-of select="subfield[@code='u']"/></field>
      </xsl:when>
      <xsl:when test="@ind2='9'">
        <field name="url"><xsl:value-of select="subfield[@code='u']"/></field>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="datafield[@tag='246']">
    <field name="alternateTitle"><xsl:call-template name="subfieldSelect"></xsl:call-template></field>
  </xsl:template>

  <xsl:template match="datafield[@tag='AF1']">
    <field name="library">
      <xsl:value-of select="subfield[@code='a']"/>
    </field>
  </xsl:template>

  <xsl:template name="subfieldSelect">
    <xsl:param name="codes">abcdefghijklmnopqrstuvwxyz</xsl:param>
    <xsl:param name="delimeter">
      <xsl:text> </xsl:text>
    </xsl:param>
    <xsl:variable name="str">
      <xsl:for-each select="subfield">
        <xsl:if test="contains($codes, @code)">
          <xsl:value-of select="text()"/>
          <xsl:value-of select="$delimeter"/>
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>
    <xsl:value-of select="substring($str,1,string-length($str)-string-length($delimeter))"/>
  </xsl:template>


</xsl:stylesheet>